﻿namespace System.ServiceModel.DomainServices.Client
{
    using System;
    using System.Reflection;

    internal class EntityVisitor
    {
        public virtual void Visit(Entity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            foreach (PropertyInfo info in entity.MetaType.AssociationMembers)
            {
                if (typeof(IEntityCollection).IsAssignableFrom(info.PropertyType))
                {
                    this.VisitEntityCollection((IEntityCollection) info.GetValue(entity, null), info);
                }
                else
                {
                    IEntityRef entityRef = entity.GetEntityRef(info.Name);
                    this.VisitEntityRef(entityRef, entity, info);
                }
            }
        }

        protected virtual void VisitEntityCollection(IEntityCollection entityCollection, PropertyInfo propertyInfo)
        {
        }

        protected virtual void VisitEntityRef(IEntityRef entityRef, Entity parent, PropertyInfo propertyInfo)
        {
        }
    }
}

